package com.fs.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.mapper.UserAddrMapper;
import com.fs.pojo.UserAddr;
import com.fs.vo.CommRequestVO;
import com.fs.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class UserAddrServiceImpl extends ServiceImpl<UserAddrMapper,UserAddr> implements UserAddrService {
    @Autowired
    private UserAddrMapper userAddrMapper;

    @Transactional
    @Override
    public ResultVO add(UserAddr entity) {
        if(entity==null)return ResultVO.fail();
        if(entity.getUserId()==null)return ResultVO.fail("用户参数有误");
        if(entity.getUsername()==null) return ResultVO.fail("收货人不能为空");
        if(entity.getPhone()==null)return ResultVO.fail("联系方式不能为空");
        if(entity.getAddr()==null)return ResultVO.fail("详细地址不能为空");
        if(entity.getProvice()==null || entity.getCity()==null || entity.getZone()==null){
            return ResultVO.fail("请选择省市区");
        }

        //如果选择默认，则将其他的地址默认字段改成false
        if(entity.getDefaultAddr()){
            UserAddr entity1 = new UserAddr();
            entity1.setDefaultAddr(false);
            QueryWrapper<UserAddr> queryWrapper = new QueryWrapper();
            queryWrapper.eq("user_id",entity.getUserId());
            userAddrMapper.update(entity1,queryWrapper);
        }

        //地址标签

        //日期
        Date date = new Date();
        entity.setCreated(date)
                .setUpdated(date);
        int i = userAddrMapper.insert(entity);
        if(i>0){
            return ResultVO.success();
        }

        return ResultVO.fail("添加失败");
    }

    @Transactional
    @Override
    public ResultVO edit(UserAddr entity) {
        int i = userAddrMapper.updateById(entity);
        return ResultVO.success();
    }

    @Override
    @Transactional
    public ResultVO list(CommRequestVO request) {
        if(request==null)return ResultVO.fail();

        int pageNum = request.getPageNum();
        int pageSize = request.getPageSize();

        if(pageNum<1)pageNum=1;
        if(pageSize<1)pageNum=10;

        Page<UserAddr> page = new Page(pageNum, pageSize);


        QueryWrapper<UserAddr> queryWrapper = new QueryWrapper();
        queryWrapper.like(StrUtil.isNotBlank(request.getQuery()),"username",request.getQuery())
                .orderByDesc("id");
        page=userAddrMapper.selectPage(page,queryWrapper);
        request.setTotal(page.getTotal()).setRows(page.getRecords());

        return ResultVO.success(request);
    }

    @Override
    @Transactional
    public ResultVO delById(Integer id) {
        userAddrMapper.deleteById(id);
        return ResultVO.success();
    }

    @Transactional
    @Override
    public ResultVO addAddr(UserAddr addr,Integer userId) {

        //检测地址个数
        Integer addrNum = userAddrMapper.getAddrNum(userId);
        if(addrNum>=10)return ResultVO.fail("最多有10个地址，请清理后再添加");


        if (StrUtil.isBlank(addr.getUsername()))return ResultVO.fail("请填写收货人姓名");
        if (StrUtil.isBlank(addr.getPhone()))return ResultVO.fail("请填写收货人联系方式");
        if (StrUtil.isBlank(addr.getAddr()))return ResultVO.fail("请填写收货详细地址");
        // todo 检测手机号格式

        if(-99 == addr.getProvice())return ResultVO.fail("请选择省或直辖市");
        if(-99==addr.getCity())return ResultVO.fail("请选择城市");
        if(-99==addr.getZone())return ResultVO.fail("请选择区域");
        if(-99==addr.getStreet())return ResultVO.fail("请选择街道");

        //是否是默认
        if(addr.getDefaultAddr()){
            userAddrMapper.updateDefaultAddr(userId);
        }

        Date now = new Date();
        addr.setUpdated(now).setUserId(userId);

        if(addr.getId()==null){
            addr.setCreated(now);
            userAddrMapper.insert(addr);
            return ResultVO.success().setMsg("添加成功");
        }else{
            //查找是否是自己的id
            QueryWrapper<UserAddr> wrapper=new QueryWrapper();
            wrapper.eq("user_id",userId)
                    .eq("id",addr.getId());
            UserAddr entity = userAddrMapper.selectOne(wrapper);
            if(entity==null)return ResultVO.fail("id有误");
            userAddrMapper.updateById(addr);
            return ResultVO.success().setMsg("更新成功");
        }

    }

    @Override
    public ResultVO addrList(Integer userId) {
//        QueryWrapper<UserAddrEntity> wrapper=new QueryWrapper<>();
//        wrapper.eq("user_id",userId)
//        .orderByDesc("default_addr","id");
//        List<UserAddrEntity> list = this.list(wrapper);
        List<UserAddr> list = userAddrMapper.getAddrList(userId);
        return ResultVO.success(list);
    }

    @Transactional
    @Override
    public ResultVO addrDel(Integer id, Integer userId) {
        QueryWrapper<UserAddr> wrapper=new QueryWrapper();
        wrapper.eq("user_id",userId)
                .eq("id",id);
        userAddrMapper.delete(wrapper);
        return ResultVO.success();
    }

}
